#include "discovery_diagnosis.h"

#include <algorithm>
#include <future>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#include "utils_file_ex.h"
#include "utils_string_ex.h"
#include "utils_string_ex.h"
#include "utils_log.h"
#include "common_shell.h"
#include "discovery_client.h"

namespace El {
namespace Discovery {

DiscoveryDiagnosis &DiscoveryDiagnosis::GetInstance()
{
    static DiscoveryDiagnosis instance;
    return instance;
}

bool DiscoveryDiagnosis::Start()
{
    Common::Shell::GetInstance().RegisterCommand(
        "discovery", std::bind(&DiscoveryDiagnosis::Discovery, this, std::placeholders::_1));
    return true;
}

void DiscoveryDiagnosis::Discovery(const std::string &args)
{
    std::vector<std::string> vec_args;
    Utils::Split(args, " ", vec_args);
    if (vec_args.size() < 1) {
        EL_ERROR("Invalid args: {}", args.c_str());
        return;
    }

    std::string text = "";
    if (vec_args[0] == "start") {
        int32_t handle = IDiscoveryClient::GetInstance().Register(
            [](const nlohmann::json &info) { EL_INFO("IDiscoveryClient info {}", info.dump(4).c_str()); });
        IDiscoveryClient::GetInstance().Start();
        EL_INFO("IDiscoveryClient Start");
        std::this_thread::sleep_for(std::chrono::seconds(5));
        IDiscoveryClient::GetInstance().Stop();
        EL_INFO("IDiscoveryClient Stop");
        IDiscoveryClient::GetInstance().Unregister(handle);
        EL_INFO("IDiscoveryClient Unregister");
    }
}

} // namespace Discovery
} // namespace El